﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BaseSet.Entity.Common.Text
{
    /// <summary>
    /// 格式校验 已整理
    /// </summary>
    public class Check
    {
        #region -- 数据类型校验 --

        /// <summary>
        /// 校验是否是整数 待改进
        /// </summary>
        /// <param name="str">要校验的字符串</param>
        /// <returns>true:是整数flase:不是整数</returns>
        public static bool IsInteger(string str)
        {
            str = str + "";
            if (str == null || str.Length == 0)
            {
                return false;
            }
            System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();

            byte[] bytestr = ascii.GetBytes(str);

            foreach (byte c in bytestr)
            {
                if (c < 48 || c > 57)
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 判断字符串是否是日期格式
        /// </summary>
        /// <param name="str">要判断的字符串</param>
        /// <returns>true:是日期格式false:不是日期格式</returns>
        public static bool IsDate(string str)
        {
            str = str + "";
            if (str == null || str.Length == 0)
            {
                return false;
            }
            try
            {
                DateTime dt = Convert.ToDateTime(str);
                return true;
            }
            catch
            {

                return false;
            }
        }

        #endregion

        #region -- 数据格式校验 （电话、手机、邮箱、邮编、IP） --

        /// <summary>
        /// 校验电话号码格式
        /// </summary>
        /// <param name="tel">电话号码</param>
        /// <returns>true:是正确的格式 false:是错误的格式或者为空</returns>
        public static bool IsTelephone(string tel)
        {
            string pattern = @"^(\(\d{3}\)|\d{3}-|\(\d{4}\)|\d{4}-)?(\d{8}|\d{7})[-]{0,1}\d{0,}$";
            bool flag = Regular.Regular.IsMatch(tel, pattern);
            return flag;
        }

        /// <summary>
        /// 校验手机号码
        /// </summary>
        /// <param name="mobile">手机号码</param>
        /// <returns>true:是正确的格式 false:是错误的格式或者为空</returns>
        public static bool IsMobile(string mobile)
        {
            //string pattern = @"^(1[35]\d{9}|189\d{8})$";
            string pattern = @"^1\d{10}$";
            bool flag = Regular.Regular.IsMatch(mobile, pattern);
            return flag;
        }

        /// <summary>
        /// 校验邮箱地址格式
        /// </summary>
        /// <param name="email">邮箱地址</param>
        /// <returns>true:是正确的格式 false:是错误的格式或者为空</returns>
        public static bool IsEmail(string email)
        {
            string pattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            bool result = Regular.Regular.IsMatch(email, pattern);
            return result;
        }

        /// <summary>
        /// 校验邮编
        /// </summary>
        /// <param name="postcode"></param>
        /// <returns></returns>
        public static Boolean IsPostcode(String postcode)
        {
            string pattern = @"^\d{6}$";
            bool result = Regular.Regular.IsMatch(postcode, pattern);
            return result;
        }

        /// <summary>
        /// 校验IP地址 含端口号的 会被判断为非IP地址 未测试
        /// </summary>
        /// <param name="IPAddress">IP地址</param>
        /// <returns>true:是IP地址 false:不是IP地址</returns>
        public static bool IsValidIP(string IPAddress)
        {
            if (IPAddress.Contains(":"))//不合理
                return false;

            string Reg = @"(^\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$";

            bool result = Regular.Regular.IsMatch(IPAddress, Reg);

            return result;
        }

        #endregion
    }
}